using System.Collections;
using System.Collections.Generic;
using EventUtils;
using UnityEngine;
using UnityEngine.UI;

namespace View.Instant
{
    public class Game2View : View
    {
        // public Button mbtnClose;
        // public Text mtxtTimes;
        private Button mbtnStop;
        private GameObject mobjStart;
        private GameObject mobjStop;
        private Text mtxtCardNum;
        private Text mtxtTime;
        private Button mbtnAddTime;
        private GameObject mobjAddTime;
        
        private List<MJCS> mFanpaiList = new List<MJCS>(){};
        private Vector3 leftup = new Vector3(-2, 1, 2);
        private float stap = 0.5f;
        private List<MJCS> mCardNumList = new List<MJCS>();
        // private bool mGamePause = false;
        private int mCardNum = 0;
        private int mGameTimer = 0;
        private int mJiashiCount = 0;
        public override void OnLoad()
        {
            base.OnLoad();
            mbtnStop = transform.Find("Up/stop").GetComponent<Button>();
            mobjStart = transform.Find("Up/stop/start").gameObject;
            mobjStop = transform.Find("Up/stop/stop").gameObject;
            mtxtCardNum = transform.Find("Up/mj/txtCount").GetComponent<Text>();
            mtxtTime = transform.Find("Up/time/txtTime").GetComponent<Text>();
            mbtnAddTime = transform.Find("Up/time").GetComponent<Button>();
            mobjAddTime = transform.Find("Up/time/jia").gameObject;

            var playerproitem = GameHelp.OnGetSelfPlayerPropItemTable();
            for (int i = 0; i < playerproitem.Count; i++)
            {
                if (playerproitem[i].enumid == PropItemEnum.jiashi)
                {
                    mJiashiCount++;
                }
            }
            mobjAddTime.SetActive(mJiashiCount<=0);
            
            
            AddClickListener(mbtnStop, () =>
            {
                GameHelp.GamePause = !GameHelp.GamePause;
                    
                if (GameHelp.GamePause)
                {
                    GameHelp.CanRayCast = false;
                    UIManager.Instance.Show(ViewName.GamePauseView, ViewLayer.Middle);
                }
            });
            
            AddClickListener(mbtnAddTime, () =>
            {
                Debug.Log("添加时间");
                if (mJiashiCount > 0)
                {
                    var proptable = GameLocalInfo.OnGetPropItemTableById(PropItemEnum.jiashi);
                    if (proptable != null)
                    {
                        mGameTimer += proptable.useadd;
                    }

                    mGameTimer--;

                    Debug.Log("添加时间"+ GameHelp.GameTimer+","+proptable.useadd);
                    GameHelp.OnRemoveSelfPlayerPropItemById(new List<PropItemEnum>(){PropItemEnum.jiashi});
                    
                    mobjAddTime.SetActive(mJiashiCount<=0);
                }
                else
                {
                    //打开，提问要不要去看广告界面
                    GameHelp.GamePause = true;
                    GameHelp.CanRayCast = false;
                    UIManager.Instance.Show(ViewName.SeeAdvView, ViewLayer.Middle,PropItemEnum.jiashi);
                }
            });
        }

        private float leijishijian = 0;
        void Update(float addtime)
        {
            mobjStart.SetActive(GameHelp.GamePause);
            mobjStop.SetActive(!GameHelp.GamePause);
            
            if (GameHelp.GamePause == false)
            {
                leijishijian += Time.deltaTime;
                if (leijishijian >= 1)
                {
                    mGameTimer--;
                    OnXuliehuaTimer();
                    leijishijian -= 1;

                    if (mGameTimer <= 0)
                    {
                        //弹出看广告
                        GameHelp.GamePause = true;
                        GameHelp.CanRayCast = false;
                        UIManager.Instance.Show(ViewName.GameEndView, ViewLayer.Game,0);
                    }
                }
            }   
        }
        
        private void OnXuliehuaTimer()
        {
            if (mGameTimer >= 0)
            {
                string shijian = "";
                long hour = (int)((mGameTimer) / 3600);
                long min = (int)((mGameTimer - hour * 3600) / 60);
                long sen = (mGameTimer - hour * 3600 - min * 60);

                if (hour > 0)
                {
                    shijian += hour + "h:";
                }
            
                if (hour > 0 || min > 0)
                {
                    if (min <= 9)
                    {
                        shijian += string.Format("0{0}:", min);
                    }
                    else
                    {
                        shijian += string.Format("{0}:", min);
                    }
                }
                if (sen >= 0)
                {
                    if (sen <= 9)
                    {
                        shijian += string.Format("0{0}", sen);
                    }
                    else
                    {
                        shijian += string.Format("{0}", sen);
                    }
                }

                mtxtTime.text = shijian;
            }
            else
            {
                //游戏结束：
                OnCheckGameEnd();
            }
        }

        //游戏结束
        private void OnCheckGameEnd()
        {
            
        }
        
        private void OnOptSetCard(ClickMjInfo info)
        {
            info.mjcs.OnShowCardFaseEx(() =>
            {
                // Debug.LogError("shishenmpai:"+info.mjNum+","+info.mjNumIndex+",."+mCardNumList.Count);
                //如果到三张了，需要检测是不是相同的牌
                for (int i = 0; i < mCardNumList.Count; i++)
                {
                    if (mCardNumList[i].mMJCardNum == info.mjNum && mCardNumList[i].mMJCardNumIndex == info.mjNumIndex)
                    {
                        return;
                    }
                }
                mCardNumList.Add(info.mjcs);
                
                if (mCardNumList.Count >= 3)
                {
                    // Debug.Log("加入的牌："+mCardNumList[0].mMJCardNum +","+ mCardNumList[1].mMJCardNum +","+mCardNumList[2].mMJCardNum);
                    if (mCardNumList[0].mMJCardNum == mCardNumList[1].mMJCardNum && mCardNumList[1].mMJCardNum == mCardNumList[2].mMJCardNum)
                    {
                        //需要消失
                        for (int i = 0; i < mCardNumList.Count; i++)
                        {
                            mCardNumList[i].OnSetDisLive();
                        }
                        
                        mCardNum -= mCardNumList.Count;
                        mtxtCardNum.text = mCardNum.ToString();
                    }
                    else
                    {
                        //翻回去
                        for (int i = 0; i < mCardNumList.Count; i++)
                        {
                            mCardNumList[i].OnShowCardFase();
                        }
                    }
                    mCardNumList.Clear();

                    //如果全部翻面就进行到下一关
                    if (MJCtrl.Instance().OnTableHaveLiveCard() == false)
                    {
                        TaskManager.OnAddTaskNumByTaskType(TaskTypeEnum.pass);
                        GameHelp.GameLevel++;
                        if (GameHelp.GameLevel >= GameHelp.GameShowCardRuleCountList.Length)
                        {
                            GameHelp.GameLevel = GameHelp.GameShowCardRuleCountList.Length - 1;
                        }
                        GameLocalInfo.OnGetCurTypeLevel(true);
                        EventCenter.BroadCast(EventUtils.EventType.EVENT_GAME_START);
                    }
                }
            });
        }

        private void OnSeeAdvEnd(PropItemEnum enumid)
        {
            if (enumid == PropItemEnum.jiashi)
            {
                mGameTimer += GameHelp.GameTimer;
            }
            GameHelp.GamePause = false;
            OnXuliehuaTimer();
        }
        
        public void OnGameStart()
        {
            //游戏开始 关卡 level
            OnStartGame(GameHelp.GameShowCardRuleCountList[GameHelp.GameLevel]);
        }
        public override void OnShow(params object[] args)
        {
            base.OnShow();
            GameHelp.mFarmeBox.SetActive(false);
            CardCao.Ins.OnShowCardCao(false);
            Utils.OnUpdate += Update;
            EventCenter.AddListener<ClickMjInfo>(EventUtils.EventType.EVENT_OPT_CLICK_CARD,OnOptSetCard);
            EventCenter.AddListener<PropItemEnum>(EventUtils.EventType.EVENT_ADV_END_TO_GAME,OnSeeAdvEnd);
            EventCenter.AddListener(EventUtils.EventType.EVENT_GAME_START,OnGameStart);

            GameHelp.GamePause = false;
            mobjStart.SetActive(GameHelp.GamePause);
            mobjStop.SetActive(!GameHelp.GamePause);
            
            // GameHelp.GameLevel = 1;
            GameHelp.GameLevel = GameLocalInfo.OnGetCurTypeLevel();
            EventCenter.BroadCast(EventUtils.EventType.EVENT_GAME_START);
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
            Utils.OnUpdate -= Update;
            if (gameObject.activeSelf)
            {
                EventCenter.RemoveListener<ClickMjInfo>(EventUtils.EventType.EVENT_OPT_CLICK_CARD,OnOptSetCard);
                EventCenter.RemoveListener<PropItemEnum>(EventUtils.EventType.EVENT_ADV_END_TO_GAME,OnSeeAdvEnd);
                EventCenter.RemoveListener(EventUtils.EventType.EVENT_GAME_START,OnGameStart);
            }
        }

        private void OnStartGame(int mjcount)
        {
            // Debug.LogError("当前 2 关卡："+GameHelp.GameLevel);
            GameHelp.OnInitRoom();
            
            mFanpaiList = GameHelp.OnResetMjPos(mjcount);
            
            mGameTimer = GameHelp.GameTimer;
            mCardNum = mFanpaiList.Count;
            mtxtCardNum.text = mCardNum.ToString();
            GameHelp.GamePause = false;
            GameHelp.CanRayCast = true;
        }
    }
}
